package com.lanou3g.ptp.api.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.lanou3g.ptp.api.resolver.annotation.SsoTeacher;
import com.lanou3g.ptp.entity.Teacher;
import com.lanou3g.ptp.entity.Warning;
import com.lanou3g.ptp.entity.WarningPlan;
import com.lanou3g.ptp.utils.PtpResult;
import com.lanou3g.ptp.utils.PtpResultUtils;
import com.lanou3g.ptp.warning.service.WarningService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

@Api(description = "预警接口")
@RestController
public class WarningController {

    @Reference
    private WarningService warningService;

    @ApiOperation("预警方案列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数"),
            @ApiImplicitParam(name = "size", value = "数量"),
            @ApiImplicitParam(name = "type", value = "预警方案类型（0签到预警，1日志预警）")
    })
    @GetMapping("/warning-plans")
    public PtpResult listWarningPlans(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                      @RequestParam(value = "size", defaultValue = "10") Integer size,
                                      Integer type) {
        PageInfo<WarningPlan> warningPlans = warningService.findPlansByType(type, page, size);
        return PtpResultUtils.ok(warningPlans);
    }

    @ApiOperation("通过ID获取预警方案")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "warningPlanId", value = "预警方案ID")
    })
    @GetMapping("/warning-plan/{warningPlanId}")
    public PtpResult findPlanById(@PathVariable Long warningPlanId) {
        WarningPlan warningPlan = warningService.findPlanById(warningPlanId);
        return warningPlan != null ? PtpResultUtils.ok(warningPlan) : PtpResultUtils.error(500);
    }

    @ApiOperation("新增预警方案")
    @PostMapping("/warning-plan")
    public PtpResult postWarningPlan(@RequestBody WarningPlan warningPlan) {
        boolean success = warningService.addPlan(warningPlan);
        return success ? PtpResultUtils.ok() : PtpResultUtils.error(500);
    }

    @ApiOperation("删除预警方案")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "warningPlanId", value = "预警方案ID")
    })
    @DeleteMapping("/warning-plan/{warningPlanId}")
    public PtpResult deletePlanById(@PathVariable Long warningPlanId) {
        boolean success = warningService.deletePlanById(warningPlanId);
        return success ? PtpResultUtils.ok() : PtpResultUtils.error(500);
    }

    @ApiOperation("修改预警方案")
    @PutMapping("/warning-plan/{warningPlanId}")
    public PtpResult updateWarningPlan(@RequestBody WarningPlan warningPlan,
                                       @PathVariable Long warningPlanId) {
        warningPlan.setId(warningPlanId);
        boolean success = warningService.updatePlanById(warningPlan);
        return success ? PtpResultUtils.ok() : PtpResultUtils.error(500);
    }

    @ApiOperation("预警信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数"),
            @ApiImplicitParam(name = "size", value = "数量"),
            @ApiImplicitParam(name = "token", value = "用户码", required = true, paramType = "header")
    })
    @GetMapping("/warnings")
    public PtpResult listWarningInfos(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                      @RequestParam(value = "size", defaultValue = "10") Integer size,
                                      @ApiIgnore @SsoTeacher Teacher teacher) {
        PageInfo<Warning> warnings = warningService.findWarningsByTeacher(teacher, page, size);
        return PtpResultUtils.ok(warnings);
    }
}
